package cn.adolf.carmaster;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cgutman.adblib.AdbCrypto;

import cn.adolf.carmaster.cgutman.AdbUtils;
import cn.adolf.carmaster.cgutman.ui.Dialog;
import cn.adolf.carmaster.cgutman.ui.SpinnerDialog;
import cn.adolf.carmaster.helper.ShareManager;
import cn.adolf.carmaster.ui.AboutFragment;
import cn.adolf.carmaster.ui.HelpFragment;
import cn.adolf.carmaster.ui.MenuActivity;

public class CarMasterActivity extends AppCompatActivity {
    private static final String TAG = CarMasterActivity.class.getSimpleName();

    private EditText mIpAddress;
    private EditText mPort;
    private Button mBtnConnect;
    SpinnerDialog keygenSpinner;
    private Activity activity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        setContentView(R.layout.activity_car_master);
        initView();

        initCryptoConfig();
    }

    private void initCryptoConfig() {
        AdbCrypto crypto = AdbUtils.readCryptoConfig(getFilesDir());
        if (crypto == null) {
            /* We need to make a new pair */
            keygenSpinner = SpinnerDialog.displayDialog(this,
                    "生成 RSA 秘钥",
                    "生成中...",
                    true);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    AdbCrypto crypto;

                    crypto = AdbUtils.writeNewCryptoConfig(getFilesDir());
                    keygenSpinner.dismiss();

                    if (crypto == null) {
                        Dialog.displayDialog(activity, "生成 RSA 秘钥",
                                "生成失败！！",
                                true);
                        return;
                    }

                    Dialog.displayDialog(activity, "生成 RSA 秘钥",
                            "生成成功",
                            false);
                }
            }).start();
        }
    }

    private void initView() {
        mIpAddress = findViewById(R.id.ipAddress);
        mPort = findViewById(R.id.port);
        mBtnConnect = findViewById(R.id.btnConnect);

        mIpAddress.setText(ShareManager.getInstance().getIp());
        mPort.setText(ShareManager.getInstance().getPost());

        mBtnConnect.setOnClickListener(v -> {
            String ip = mIpAddress.getText().toString();
            int port = Integer.parseInt(mPort.getText().toString());
            if (TextUtils.isEmpty(ip)) {
                Toast.makeText(this, "请输入正确的ip地址", Toast.LENGTH_SHORT).show();
            } else if (port <= 0 || port > 65535) {
                Toast.makeText(this, "请输入正确的端口号", Toast.LENGTH_SHORT).show();
            } else {
                Intent i = new Intent(this, CommandActivity.class);
                i.putExtra(CommandActivity.EXTRA_IP, ip);
                i.putExtra(CommandActivity.EXTRA_PORT, port);
                startActivity(i);
                // doConnect(ip, port);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_memu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_help:
                Intent help = new Intent(this, MenuActivity.class);
                help.putExtra(MenuActivity.EXTRA_TYPE, MenuActivity.MENU_HELP);
                startActivity(help);
                break;
            case R.id.menu_about:
                Intent about = new Intent(this, MenuActivity.class);
                about.putExtra(MenuActivity.EXTRA_TYPE, MenuActivity.MENU_ABOUT);
                startActivity(about);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}